let Disp = require("@/utils/Dispatcher.js");

let msgPackager = require("./msgpackager.js");

let msgType = require("./msgtype.js");

let msgStorage = new Disp();

let disp = require("@/utils/broadcast.js");
const defaultObj = (receiveMsg, type)=> {
	return {
		id: receiveMsg.id,
		type,
		body: {
			id: receiveMsg.id,
			from: receiveMsg.from,
			length: receiveMsg.file_length,
			to: receiveMsg.to,
			type: receiveMsg.type,
			ext: receiveMsg.ext,
			chatType: receiveMsg.type,
			toJid: "",
			body:{},
			data:receiveMsg
		}
	}
}
//图片
msgStorage.img = (receiveMsg, type)=> {
	const item = defaultObj(receiveMsg, type);
	item.body.body = {
		type: type,
		url: receiveMsg.url,
		filename: receiveMsg.filename,
		filetype: receiveMsg.filetype,
		size: {
			width: receiveMsg.width,
			height: receiveMsg.height
		}
	}
	return item
}
//文字或emjoy
msgStorage.txtEmjoy = (receiveMsg, type)=> {
	const item = defaultObj(receiveMsg, type);
	item.body.body =  {
		type: type,
		msg: receiveMsg.data
	}
	item.value = receiveMsg.data
	return item
}
//文件
msgStorage.file = (receiveMsg, type)=> {
	const item = defaultObj(receiveMsg, type);
	item.body.body =  {
		type: type,
		url: receiveMsg.url,
		filename: receiveMsg.filename,
		msg: "当前不支持此格式消息展示"
	}
	item.value = receiveMsg.data
	return item
}
//语音及视频
msgStorage.audioVideo = (receiveMsg, type)=> {
	const item = defaultObj(receiveMsg, type);
	item.accessToken = receiveMsg.token || receiveMsg.accessToken
	item.body.body =  {
		type,
		...receiveMsg
	}
	return item
}
//地址
msgStorage.location = (receiveMsg, type)=> {
	const item = defaultObj(receiveMsg, type);
	item.accessToken = receiveMsg.token || receiveMsg.accessToken
	item.body.body =  {
		type,
		...receiveMsg
	}
	return item
}
//自定义邀请上车
msgStorage.custom = (receiveMsg, type)=> {
	const item = defaultObj(receiveMsg, type);
	item.body.body =  {
		type,
		msg: receiveMsg.data
	}
	item.value = receiveMsg.data
	return item
}
//系统接受消息
msgStorage.system_tips = (receiveMsg, type)=> {
	const item = defaultObj(receiveMsg, type);
	item.body.body =  {
		type,
		msg: receiveMsg.data
	}
	item.value = receiveMsg.data
	return item
}
msgStorage.saveReceiveMsg = (receiveMsg, type)=> {
	let name = type;
	if(type == msgType.TEXT || type == msgType.EMOJI){
		name = 'txtEmjoy'
	}
	if(type == msgType.AUDIO || type == msgType.VIDEO){
		name = 'audioVideo'
	}
	if(!msgStorage[name]) return false;
	const sendableMsg = msgStorage[name](receiveMsg, type);
  msgStorage.saveMsg(sendableMsg, type, receiveMsg);
};

msgStorage.saveMsg = function (sendableMsg, type, receiveMsg) {
  let me = this;
  let myName = uni.getStorageSync("myUsername");
  let sessionKey; // 仅用作群聊收消息，发消息没有 receiveMsg

  if (receiveMsg && receiveMsg.type == "groupchat") {
    sessionKey = receiveMsg.to + myName;
  } // 群聊发 & 单发 & 单收
  else {
		const bodyName = sendableMsg.body.from == myName ? sendableMsg.body.to : sendableMsg.body.from;
    sessionKey = bodyName + myName;
  }
  sessionKey = sessionKey.toLocaleLowerCase()
  let curChatMsg = uni.getStorageSync(sessionKey) || [];
  let renderableMsg = msgPackager(sendableMsg, type, myName,receiveMsg);

  if (type == msgType.AUDIO) {
    renderableMsg.msg.length = sendableMsg.body.length;
    renderableMsg.msg.token = sendableMsg.accessToken;
  }

  curChatMsg.push(renderableMsg); //console.log('renderableMsgrenderableMsg', renderableMsg)

  if (type == msgType.AUDIO) {
    renderableMsg.msg.token = sendableMsg.accessToken; //如果是音频则请求服务器转码
  }
	const save = (renderableMsg,type,sessionKey,curChatMsg)=> {
		uni.setStorage({
			key: sessionKey,
			data: curChatMsg,
			success() {
				(type == msgType.AUDIO || type == msgType.VIDEO) && disp.fire('em.chat.audio.fileLoaded');
				me.fire("newChatMsg", renderableMsg, type, curChatMsg, sessionKey);
			}
		});
	}
	console.log(sessionKey)
  save(renderableMsg,type,sessionKey,curChatMsg);
};
module.exports = msgStorage;